package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.zhgd.domain.bo.NSysStationAsphaltMaterialBo;
import com.ruoyi.zhgd.domain.vo.NSysStationAsphaltMaterialVo;
import com.ruoyi.zhgd.domain.NSysStationAsphaltMaterial;
import com.ruoyi.zhgd.mapper.NSysStationAsphaltMaterialMapper;
import com.ruoyi.zhgd.service.INSysStationAsphaltMaterialService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站固定材料价格Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NSysStationAsphaltMaterialServiceImpl implements INSysStationAsphaltMaterialService {

    private final NSysStationAsphaltMaterialMapper baseMapper;

    /**
     * 查询沥青拌合站固定材料价格
     */
    @Override
    public NSysStationAsphaltMaterialVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站固定材料价格列表
     */
    @Override
    public TableDataInfo<NSysStationAsphaltMaterialVo> queryPageList(NSysStationAsphaltMaterialBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NSysStationAsphaltMaterial> lqw = buildQueryWrapper(bo);
        Page<NSysStationAsphaltMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站固定材料价格列表
     */
    @Override
    public List<NSysStationAsphaltMaterialVo> queryList(NSysStationAsphaltMaterialBo bo) {
        LambdaQueryWrapper<NSysStationAsphaltMaterial> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NSysStationAsphaltMaterial> buildQueryWrapper(NSysStationAsphaltMaterialBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NSysStationAsphaltMaterial> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPowderPrice() != null, NSysStationAsphaltMaterial::getPowderPrice, bo.getPowderPrice());
        lqw.eq(bo.getAsphaltPrice() != null, NSysStationAsphaltMaterial::getAsphaltPrice, bo.getAsphaltPrice());
        return lqw;
    }

    /**
     * 新增沥青拌合站固定材料价格
     */
    @Override
    public Boolean insertByBo(NSysStationAsphaltMaterialBo bo) {
        NSysStationAsphaltMaterial add = BeanUtil.toBean(bo, NSysStationAsphaltMaterial.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站固定材料价格
     */
    @Override
    public Boolean updateByBo(NSysStationAsphaltMaterialBo bo) {
        NSysStationAsphaltMaterial update = BeanUtil.toBean(bo, NSysStationAsphaltMaterial.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NSysStationAsphaltMaterial entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站固定材料价格
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
